package transmit

import (
	"fmt"
	"net"
	"os"

	"myOJ/coderun"

	"github.com/google/uuid"
)

//获取Code文件
var fileroot string = "excefileroot/"

func getCode(conn net.Conn) {
	buf := make([]byte, 8196, 8196)
	fileLen, err := conn.Read(buf)
	if err != nil {
		fmt.Println(err)
		return
	}

	randomSeed := uuid.New().String()
	filename := fileroot + randomSeed + ".py"
	fn, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0777)
	defer fn.Close()

	_, err = fn.Write(buf[:fileLen])
	if err != nil {
		fmt.Println(err)
		return
	}
	args := make([]string, 0, 10)
	args = append(args, filename)
	result := coderun.Runpy(args)
	returnResult(conn, result)
}

//获取新连接
func Getconn(conn net.Conn) {
	getCode(conn)
}
